<?php

class Controller_Device extends Controller_Site {
    public function action_index() {
        $categories = ORM::factory('category')->order_by('name')->find_all();
        foreach($categories as $key => $category) {
            $categoty_view = View::factory('device/category')->set('category', $category);
            $devices = '';

            foreach($category->Devices->find_all() as $device) {
                $devices .= View::factory('device/item')->set('device', $device);
            }

            $categoty_view->set('devices', $devices);
            $this->content .= $categoty_view;
        }
    }

    public function action_new() {
        $form = View::factory('device/form')->set('action', URL::site('device/new'));

        if($this->request->method() == 'POST') {
            try {
                $device = new Model_Device();
                $form->set('device', $device);
                $this->processForm($device);

                $this->setFlash('notice', 'Sikeresen felvetted az új eszközt!');
                $this->request->redirect(URL::site('device/list'));
            }
            catch (ORM_Validation_Exception $e) {
                unlink(realpath(Upload::$default_directory).DIRECTORY_SEPARATOR.$device->image);
                $form->set('errors', $e->errors('models'));
            }
        }

        $this->content->set('form', $form);
    }

    protected function processForm($device, $is_image_optional = false) {
        $device->name = $this->request->post('name');
        $device->description = $this->request->post('description');
        $device->category_id = $this->request->post('category_id');

        if(!$is_image_optional) {
            $file = Validation::factory($_FILES)->rule('image', 'Upload::image', array(':value', array(640, 480)));
            if(!$file->check())
            throw new ORM_Validation_Exception('image', $file);

            $device->image = basename(Upload::save($_FILES['image']));
        }
        
        $device->save();
    }

    public function action_edit() {
        $form = View::factory('device/form')->set('action', URL::site('device/edit/' . $this->request->param('id')));
        $device = new Model_Device($this->request->param('id'));

        $form->set('device', $device);

        if($this->request->method() == 'POST') {
            try {
                $this->processForm($device, true);
                $this->setFlash('notice', 'Sikeresen módosítottad az eszközt!');
                $this->request->redirect(URL::site('device/list'));
            }
            catch (ORM_Validation_Exception $e) {
                unlink(realpath(Upload::$default_directory).DIRECTORY_SEPARATOR.$device->image);
                $form->set('errors', $e->errors('models'));
            }
        }

        $this->content->set('form', $form);
    }

    public function action_delete() {
        $device = new Model_Device($this->request->param('id'));
        try {
            $device->delete();
            $this->setFlash('notice', 'Sikeresen törölted az eszközt!');
            $this->request->redirect(URL::site('device/list'));
        }
        catch(Kohana_Exception $e) {
            $this->setFlash('error', $e->getMessage());
        }
    }

    public function action_list() {
        $devices = ORM::factory('device')->order_by('name')->find_all();
        $this->setVar('devices', $devices);
    }
}